cmake_minimum_required(VERSION 2.6)

#definition du projet
project (d7c)

set(EXECUTABLE_OUTPUT_PATH bin${CMAKE_BUILD_TYPE})
set(package_name d7c)
set(package_version svn0.2)
set(email_report alexisis-pristontale@hotmail.com)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
SET(CMAKE_VERBOSE_MAKEFILE OFF)

#recherche des bibliothéques
find_package(Gettext)
find_package(SQLite REQUIRED)

include(UseLibraries)
USE_LIBRARIES(glib2 gcrypt ircclient pthread)

#verification des bibliothéques
if (NOT HAVE_glib2)
	message(FATAL_ERROR "missing glib2 : please install it")
endif (NOT HAVE_glib2)
if (NOT HAVE_gcrypt)
	message(FATAL_ERROR "missing gcrypt : please install it")
endif (NOT HAVE_gcrypt)
if (NOT HAVE_ircclient)
	message(FATAL_ERROR "missing libircclient : please install it")
endif (NOT HAVE_ircclient)
if (NOT SQLite_FOUND)
	message(FATAL_ERROR "missing sqlite3 : please install it")
endif (NOT SQLite_FOUND)
if (NOT GETTEXT_FOUND)
	message(FATAL_ERROR "missing gettext : please install it")
endif (NOT GETTEXT_FOUND)
if (NOT HAVE_pthread)
	message(FATAL_ERROR "missing pthreads : please install it")
endif (NOT HAVE_pthread)

#inclusion des bibliothéques au projet
include_directories(
	${USE_gcrypt_INCLUDE_DIR}
	${USE_glib2_INCLUDE_DIR_1}
	${USE_glib2_INCLUDE_DIR_2}
	${USE_ircclient_INCLUDE_DIR}
	${SQLITE_INCLUDE_DIRS}
	${USE_pthreads_INCLUDE_DIRS}
)

#identification des fichiers
set(
	sources_files
	src/main.c
	src/fonction_algo.c
	src/fonction_algo.h
	src/fonction_connection.c
	src/fonction_connection.h
	src/gestion_connection.c
	src/gestion_connection.h
	src/couleurs.h
	src/special_opt.c
	src/special_opt.h
	src/fonction_threads.c
	src/fonction_threads.h
	src/trie_commandes.h
	src/trie_commandes.c
	src/aide.h
	src/struct.h
)
set(
	po_files
	po/fr.po
) 
set(pot_file po/${package_name}.pot)

#creation de l'executable
add_executable(
	d7c
	${sources_files}		
)
target_link_libraries(
	d7c
	${LIBRARIES} ${SQLITE_LIBRARIES}
)

#gestion de gettext
add_custom_target(
	update_pot
	COMMAND xgettext -d ${package_name} -s -o
		${package_name}.pot
		${CMAKE_BINARY_DIR}/${sources_files} 
		-p ./po 
		--from-code=utf-8 -j 
		--keyword=_
		--package-name=${package_name}
		--package-version=${package_version} 
		--msgid-bugs-address=${email_report}
)

#creation des fichiers mo et mise a jour des fichiers po
foreach (po_courrent ${po_files})
	get_filename_component(
		langue
		${po_courrent}
		NAME_WE
	)
	set(mo_file po/${langue}.mo)
	add_custom_command(
		TARGET update_pot
		POST_BUILD
		COMMAND msgmerge --update ./${po_courrent} ./${pot_file}
		DEPENDS ${po_courrent} ${pot_file}
	)

	message ("dependance : ${po_courrent}")
	message ("dependance : ${pot_file}")
	message ("dependance : ${langue}")
	message ("dependance : ${mo_file}")
	message ("dependance : ${po_files}")
	message ("--- new loop ---")
	add_custom_command(
		OUTPUT ${mo_file}
		COMMAND msgfmt -c ${CMAKE_CURRENT_SOURCE_DIR}/po/${po_courrent} -o ${CMAKE_CURRENT_SOURCE_DIR}/po/${langue}.mo
	)
	install(
		FILES ${langue}.mo 
		DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/po/locale/${langue}/LC_MESSAGES
		RENAME ${package_name}.mo
	)
	set(pos_courrent ${pos_courrent} ${po_courrent})
endforeach (po_courrent) 
message ("dependance : ${pos_courrent}")
add_custom_target(build_mo ALL DEPENDS ${pos_courrent})
#ajout d'un fichier po
add_custom_target(
	create_po
	COMMAND msginit -l $(LANGUAGE) -o ./po/$(LANGUAGE).po -i ./po/${package_name}.pot
)
add_subdirectory(lib)
